package com.docking.heylo.service.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import com.docking.heylo.dao.entity.Post;
import com.docking.heylo.dao.mapper.PostMapper;
import com.docking.heylo.service.IPostService;
import com.docking.heylo.web.dto.PageDto;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
public class PostServiceImpl implements IPostService {

    @Resource
    private PostMapper postMapper;

    @Value("${api.url}")
    private String apiUrl;

    @Override
    public PageInfo<Map<String, Object>> managers_list(PageDto pageDto) {
        PageHelper.startPage(pageDto.getPageNumber(), pageDto.getPageSize());
        return new PageInfo<>(postMapper.managers(pageDto.getUserPhone(), pageDto.getUserId()));
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void remove(Integer id) {
        Post post = postMapper.selectByPrimaryKey(id);
        postMapper.deleteByPrimaryKey(id);

        HttpUtil.post(apiUrl + "/public/post/remove", new HashMap<String, Object>(){{
            put("code", "post/remove" + id);
            put("userId", post.getUserId());
            put("postId", id);
        }});
    }







}
